//
//  RCSearchingMainPageVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-5-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCSearchingMainPageVC.h"
#import "IManagerBase.h"
#import "GloableEngineService.h"
#import "RCCityListVC.h"
#import "MainPageNearbyCell.h"
#import "RCMerchantListVC.h"
#import "RCFoodRecommendVC.h"
#import "RCSearchAllCityVC.h"
#import "NSString+NSString_Ex_.h"
#import "CommonDefines.h"
#import "RCSearchMoreTagsVC.h"
#import "AppDelegate.h"
#import "RCSearchCityListDataModel.h"
#import "RCSearchAllVC.h"
#import "UIColor-Expanded.h"
#import "RCRecommendFoodListViewController.h"
#import "RCDiscountMainVC.h"
#import "RCActivityDiscountViewController.h"
#import "RCTicketBookingVC.h"
#import "RCFansListVC.h"
#import "IManager+ModulePushMessage.h"
#import "NCPageControl.h"
#import "SMPageControl.h"
#import "RCPushMessageDataModel.h"
#import "NSArray+EX.h"
#import "NewWebviewPluginController.h"
#import "BDVRConfig.h"
#import "RCPointsShopViewController.h"



#define kArrowImageFile           @"image_nearby_arrow.png"
#define kSearchImageFile          @"image_search_searchBar.png"
#define kVoiceImageFile           @"image_mainPage_voice.png"
#define kSearchIconImageFile      @"image_tabbar_search_normal.png"
#define kSwitchSceneDotActiveImage     @"image_pushMessage_dot_active.png"
#define kSwitchSceneDotInActiveImage   @"image_pushMessage_dot_inactive.png"
#define kHeaderLineImageFile      @"image_pushMessage_line.png"
#define kHeaderDeleteImageFile    @"image_pushMessage_delete.png"

#define kTextPlaceHolder          @"输入商户名搜索"

#define API_KEY @"8MAxI5o7VjKSZOKeBzS4XtxO"
#define SECRET_KEY @"Ge5GXVdGQpaxOmLzc8fOM8309ATCz9Ha"

@interface RCSearchingMainPageVC ()<IManagerDelegate,UIScrollViewDelegate>

@property(nonatomic,retain) UITableView *t;
@property(nonatomic,retain) UIButton *cityButton;
@property(nonatomic,retain) UILabel *cityLabel;
@property(nonatomic,retain) UIImageView *arrowImageView;
@property(nonatomic,copy) NSString *tempCityName;
@property(nonatomic,retain) UIButton *transparentButton;
@property(nonatomic,retain) UITextField *searchTextField;
@property(nonatomic,assign) BOOL isCityLocated; //是否已经定位成功过
@property(nonatomic,assign) BOOL isCityFromSaved; //从存储获取城市信息

@property(nonatomic,assign) int64_t requestID;
@property(nonatomic,retain) UIScrollView *headerScrollView;
@property(nonatomic,retain) SMPageControl *headerPageControl;
@property(nonatomic,retain) RCPushMessageDataModel *pushDataModel;
@property(nonatomic,retain) UIView *headerView;

@end

@implementation RCSearchingMainPageVC

@synthesize t = _t;
@synthesize cityName = _cityName;
@synthesize cityButton = _cityButton;
@synthesize cityLabel = _cityLabel;
@synthesize arrowImageView = _arrowImageView;
@synthesize tempCityName = _tempCityName;
@synthesize transparentButton = _transparentButton;
@synthesize searchTextField = _searchTextField;
@synthesize isCityLocated = _isCityLocated;
@synthesize isCityFromSaved = _isCityFromSaved;
@synthesize requestID = _requestID;
@synthesize headerScrollView = _headerScrollView;
@synthesize headerPageControl = _headerPageControl;
@synthesize pushDataModel = _pushDataModel;
@synthesize headerView = _headerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter]  addObserver:self
                                                  selector:@selector(cityNameChanged:) name:@"CityNameChanged"
                                                    object:nil];
        [[NSNotificationCenter defaultCenter]  addObserver:self
                                                  selector:@selector(selectedCityChanged) name:@"SelectCityChanged"
                                                    object:nil];
        self.isCityLocated = NO;
        self.isCityFromSaved  = NO;

    }
    return self;
}

-(void) dealloc
{
    [[NSNotificationCenter defaultCenter]  removeObserver:self
                                                     name:@"CityNameChanged"
                                                   object:nil];
    [[NSNotificationCenter defaultCenter]  removeObserver:self
                                                     name:@"SelectCityChanged"
                                                   object:nil];
    self.cityLabel = nil;
    self.cityName = nil;
    self.cityButton = nil;
    self.arrowImageView = nil;
    self.tempCityName = nil;
    self.transparentButton = nil;
    self.searchTextField = nil;
    self.pushDataModel = nil;
    self.headerScrollView = nil;
    self.headerPageControl = nil;
    self.pushDataModel = nil;
    self.headerView = nil;
    self.t = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _t = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-20-44-49)];
    _t.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.viewContainer addSubview:_t];
    _t.dataSource = self;
    _t.delegate = self;
    
    
    [self createCityPlace];
    [self createSearchView];
    
}



- (void) createCityPlace
{
    if (!_cityButton) {
        _cityButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 65, 44)];
    }
    if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
        _cityButton.frame = CGRectMake(-10, 0, 65, 44);
    }
    _cityButton.backgroundColor = [UIColor clearColor];
    [_cityButton addTarget:self action:@selector(changeCity) forControlEvents:UIControlEventTouchUpInside];
    if (!_cityLabel) {
        _cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 14, 50, 16)];
    }
    _cityLabel.backgroundColor = [UIColor clearColor];
    _cityLabel.textColor = [UIColor whiteColor];
    _cityLabel.font = [UIFont fontWithName:kFontNameHeiti size:16];
    _cityLabel.shadowColor = [UIColor colorWithHex:0xffc2c2c2];
    _cityLabel.shadowOffset = CGSizeMake(0, 0.5);
    [_cityButton addSubview:_cityLabel];
    
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(55, 20, 9, 5)];
    }
    _arrowImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kArrowImageFile];
    [_arrowImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [_cityButton addSubview:_arrowImageView];
    
    
    NSString *currentCityName = [GloableEngineService sharedInstance].userCityName;
    if ([NSString stringIsNullOrEmpty:currentCityName]) {
        RCSearchCityListItem *item = [[GloableEngineService sharedInstance]getSavedSelectCityInfo];
        currentCityName = item.cityName;
        self.isCityFromSaved = YES;
    }
    
    if (![NSString stringIsNullOrEmpty:currentCityName]) {
        self.cityName = currentCityName;
        
        NSString *userCityName = [GloableEngineService sharedInstance].userCityName;
        if (userCityName != nil && [self.cityName isEqualToString:userCityName]) {
            [GloableEngineService sharedInstance].selectedCityID = nil;
        }
        
        CGSize size = [self.cityName sizeWithFont:_cityLabel.font constrainedToSize:CGSizeMake(200, 20)];
        _cityLabel.frame = CGRectMake(10, 14, size.width, 16);
        _arrowImageView.frame = CGRectMake(10+size.width+5, 20, 9, 5);
        _cityButton.frame = CGRectMake(0, 0, size.width+10+15, 44);
        if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
            _cityButton.frame = CGRectMake(-10, 0, size.width+10+15, 44);
        }
        _cityLabel.text = self.cityName;
        self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:_cityButton];
    }
    
    _requestID = [[IManager sharedInstance]requestPushMessageInfo:self];
}


- (void) createSearchView
{
    UIImageView *searchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(86, 7, 219, 30)];
    searchImageView.userInteractionEnabled = YES;
    searchImageView.backgroundColor = [UIColor clearColor];
    NSString *searchpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kSearchImageFile];
    [searchImageView setImage:[UIImage imageWithContentsOfFile:searchpath]];
    
    UIImageView *searchIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 8, 18, 18)];
    searchIconImageView.backgroundColor = [UIColor clearColor];
    NSString *searchIconpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kSearchIconImageFile];
    [searchIconImageView setImage:[UIImage imageWithContentsOfFile:searchIconpath]];
    [searchImageView addSubview:searchIconImageView];
    [searchIconImageView release];
    
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, 140, 30)];
        _searchTextField.borderStyle = UITextBorderStyleNone;
        _searchTextField.delegate = self;
        _searchTextField.placeholder = kTextPlaceHolder;
        _searchTextField.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
        _searchTextField.textAlignment = UITextAlignmentLeft;
        _searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _searchTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _searchTextField.returnKeyType = UIReturnKeySearch;
    }
    
    [searchImageView addSubview:_searchTextField];
    [searchImageView bringSubviewToFront:_searchTextField];
    
    UIButton *voicButton = [[UIButton alloc]initWithFrame:CGRectMake(169, 0, 50, 30)];
    voicButton.backgroundColor = [UIColor clearColor];
    [voicButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kVoiceImageFile ofType:nil]] forState:UIControlStateNormal];
    [voicButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kVoiceImageFile ofType:nil]] forState:UIControlStateHighlighted];
    [voicButton addTarget:self action:@selector(sdkUIRecognitionAction) forControlEvents:UIControlEventTouchUpInside];
    [searchImageView addSubview:voicButton];
    [voicButton release];
    
    self.navigationItem.titleView = searchImageView;
    [searchImageView release];
}



-(void)createTabeleHeaderView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        _headerView.backgroundColor = [UIColor colorWithHex:0xffededed];
        
        _headerScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        _headerScrollView.showsHorizontalScrollIndicator = NO;
        _headerScrollView.pagingEnabled = YES;
        _headerScrollView.delegate = self;
        _headerScrollView.backgroundColor = [UIColor clearColor];
        _headerScrollView.contentSize = CGSizeMake(320*[self.pushDataModel.itemListArray count], 50);
        NSInteger count = [self.pushDataModel.itemListArray count];
        
        for (int i =0; i < count; i++) {
            
            RCPushMessageItem *item = [self.pushDataModel.itemListArray safeObjectAtIndex:i];
            if (nil == item) {
                continue;
            }
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i*320, 10, 320, 16)];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor colorWithHex:0xff505050];
            label.font = [UIFont fontWithName:kFontNameHeiti size:16];
            label.textAlignment = UITextAlignmentCenter;
            label.text = item.itemTitle;
            [_headerScrollView addSubview:label];
            [label release];
            
            UIButton *transparnButton = [[UIButton alloc]initWithFrame:CGRectMake(i*320, 0, 278, 50)];
            transparnButton.tag = 300+i;
            transparnButton.backgroundColor = [UIColor clearColor];
            [transparnButton addTarget:self action:@selector(headerTransparentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [_headerScrollView addSubview:transparnButton];
            [transparnButton release];
            
        }
        
        [_headerView addSubview:_headerScrollView];
        
        _headerPageControl = [[SMPageControl alloc] init];
        CGFloat pageContWidth = (7+3)*count;
        _headerPageControl.frame = CGRectMake((320-pageContWidth)/2, 29, pageContWidth, 16);
        _headerPageControl.numberOfPages = count;
        _headerPageControl.indicatorMargin = 3;
        [_headerPageControl setUserInteractionEnabled:NO];
        _headerPageControl.backgroundColor=[UIColor clearColor];
        _headerPageControl.indicatorDiameter = 7;
        [_headerPageControl setPageIndicatorImage:[UIImage imageNamed:kSwitchSceneDotInActiveImage]];
        [_headerPageControl setCurrentPageIndicatorImage:[UIImage imageNamed:kSwitchSceneDotActiveImage]];
        _headerPageControl.currentPage = 0;
        [_headerView addSubview:_headerPageControl];
    }
    
    UIButton *deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(278, 0, 42, 50)];
    deleteButton.backgroundColor = [UIColor clearColor];
    [deleteButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kHeaderDeleteImageFile ofType:nil]] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(deleteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:deleteButton];
    [deleteButton release];
    
    UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 49, 320, 1)];
    lineImageView.backgroundColor = [UIColor clearColor];
    [lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kHeaderLineImageFile ofType:nil]]];
    [_headerView addSubview:lineImageView];
    [lineImageView release];
    
}

-(void)selectedCityChanged{
    
    NSString *globalCityName =[GloableEngineService sharedInstance].selectedCityName;
    if ([globalCityName isEqualToString:[GloableEngineService sharedInstance].userCityName]) {
        self.isCityLocated = YES;
    }
    if (![NSString stringIsNullOrEmpty:self.cityName] && ![NSString stringIsNullOrEmpty:globalCityName] && ![self.cityName isEqualToString:globalCityName]) {
        self.cityName = globalCityName;

        CGSize size = [self.cityName sizeWithFont:_cityLabel.font constrainedToSize:CGSizeMake(200, 20)];
        _cityLabel.frame = CGRectMake(10, 14, size.width, 16);
        _arrowImageView.frame = CGRectMake(10+size.width+5, 20, 9, 5);
        _cityButton.frame = CGRectMake(0, 0, size.width+10+15, 44);
        if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
            _cityButton.frame = CGRectMake(-10, 0, size.width+10+15, 44);
        }
        _cityLabel.text = self.cityName;
    }
    
    if ([NSString stringIsNullOrEmpty:self.cityName] && ![NSString stringIsNullOrEmpty:globalCityName]) {
        self.cityName = globalCityName;

        CGSize size = [self.cityName sizeWithFont:_cityLabel.font constrainedToSize:CGSizeMake(200, 20)];
        _cityLabel.frame = CGRectMake(10, 14, size.width, 16);
        _arrowImageView.frame = CGRectMake(10+size.width+5, 20, 9, 5);
        _cityButton.frame = CGRectMake(0, 0, size.width+10+15, 44);
        if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
            _cityButton.frame = CGRectMake(-10, 0, size.width+10+15, 44);
        }
        _cityLabel.text = self.cityName;
        self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:_cityButton];
    }
}

-(void) cityNameChanged:(NSNotification *)notification
{
    NSDictionary *dic = (NSDictionary *)[notification object];
    NSString *currentCityName = [dic objectForKey:@"CityName"];
    
    NSLog(@"dic cityname :%@",[dic objectForKey:@"CityName"]);
    if (!self.isCityFromSaved) {
        if ([NSString stringIsNullOrEmpty:self.cityName] || !self.isCityLocated) {
            self.cityName = currentCityName;
            self.isCityLocated = YES;
            [GloableEngineService sharedInstance].selectedCityID = nil;
            CGSize size = [self.cityName sizeWithFont:_cityLabel.font constrainedToSize:CGSizeMake(200, 20)];
            _cityLabel.frame = CGRectMake(10, 14, size.width, 16);
            _arrowImageView.frame = CGRectMake(10+size.width+5, 20, 9, 5);
            _cityButton.frame = CGRectMake(0, 0, size.width+10+15, 44);
            if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
                _cityButton.frame = CGRectMake(-10, 0, size.width+10+15, 44);
            }
            _cityLabel.text = self.cityName;
            self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:_cityButton];
        }else{
            if (![self.cityName isEqualToString:currentCityName]) {
                
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"切换城市" message:@"所在城市已经变更，是否切换" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alertView show];
                [alertView release];
                alertView  = nil;
                self.tempCityName = currentCityName;
                
            }
        }
    }else{
        
        //只有之前选择的是当前定位城市，这时候当前城市发生变化才提醒
        if (![NSString stringIsNullOrEmpty:self.cityName] && self.isCityLocated && ![self.cityName isEqualToString:currentCityName]) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"切换城市" message:@"所在城市已经变更，是否切换" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
            [alertView release];
            alertView  = nil;
            self.tempCityName = currentCityName;
        }
    }
    


}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        self.cityName = _tempCityName;
        [GloableEngineService sharedInstance].selectedCityID = nil;
        CGSize size = [self.cityName sizeWithFont:_cityLabel.font constrainedToSize:CGSizeMake(200, 20)];
        _cityLabel.frame = CGRectMake(10, 14, size.width, 16);
        _arrowImageView.frame = CGRectMake(10+size.width+5, 20, 9, 5);
        _cityButton.frame = CGRectMake(0, 0, size.width+10+15, 44);
        if ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0f ) {
            _cityButton.frame = CGRectMake(-10, 0, size.width+10+15, 44);
        }
        _cityLabel.text = self.cityName;
    }
}


-(void) changeCity
{
    RCCityListVC *cityListVC = [[RCCityListVC alloc] init];
    cityListVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cityListVC animated:YES];
    [cityListVC release];
}

#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (!_transparentButton) {
        _transparentButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-64-49-270)];
        _transparentButton.backgroundColor = [UIColor clearColor];
        [_transparentButton addTarget:self action:@selector(transparentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.viewContainer addSubview:_transparentButton];
    }
    _transparentButton.hidden = NO;
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }else if ([textField.text length] > 11){
        return NO;
    }
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    _transparentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:textField.text] ) {
        RCSearchAllVC *vc = [[RCSearchAllVC alloc]initWithTitle:textField.text];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
    
}


-(void)transparentButtonClicked{
    _transparentButton.hidden = YES;
    [_searchTextField resignFirstResponder];
}


#pragma
#pragma mark UITableView DataSource

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0) {
        #ifndef ServerVersion
        return 240;
        #else
        return 176;
        #endif
    }else{
        return 72;
    }
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0 ) {
        MainPageNearbyCell *cell = [[[MainPageNearbyCell alloc] init] autorelease];
        cell.delegate = self;
        return cell;
    }else if ([indexPath row] == 2 ){
        MainPageNormalCell *cell = [[MainPageNormalCell alloc] initWithTitle:@"排行榜" subTitle:@"找到最棒的商家"];
        return [cell autorelease];
    }else if([indexPath row] == 3 ){
        MainPageNormalCell *cell = [[MainPageNormalCell alloc] initWithTitle:@"搜全城" subTitle:@"按商区查询，找遍城市每个角落"];
        return [cell autorelease];
    }else if([indexPath row] == 4 ){
        MainPageNormalCell *cell = [[MainPageNormalCell alloc] initWithTitle:@"美食" subTitle:@"满足你的胃，找到你想要的"];
        return [cell autorelease];
    }else if([indexPath row] == 5 ){
        MainPageNormalCell *cell = [[MainPageNormalCell alloc] initWithTitle:@"优惠" subTitle:@"超值优惠，随意挑选，尽情享受"];
        return [cell autorelease];
    }else if([indexPath row] == 1 ){
        MainPageNormalCell *cell = [[MainPageNormalCell alloc] initWithTitle:@"积分商城" subTitle:@"海量积分商品，任你兑换"];
        return [cell autorelease];
    }
    return nil;

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 2) {
        if ([NSString stringIsNullOrEmpty:[GloableEngineService sharedInstance].selectedCityID]  || [[GloableEngineService sharedInstance].selectedCityID isEqualToString:@"0"]) {
            [RCAppDelegate alertWithCustomizeString:@"抱歉，当前城市暂未开通"];
            return;
        }
        RCDiscountMainVC *vc = [[RCDiscountMainVC alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if ([indexPath row] == 3) {
        if ([NSString stringIsNullOrEmpty:[GloableEngineService sharedInstance].selectedCityID]  || [[GloableEngineService sharedInstance].selectedCityID isEqualToString:@"0"]) {
            [RCAppDelegate alertWithCustomizeString:@"抱歉，当前城市暂未开通"];
            return;
        }
        RCSearchAllCityVC *vc = [[RCSearchAllCityVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if ([indexPath row] == 4) {
        if ([NSString stringIsNullOrEmpty:[GloableEngineService sharedInstance].selectedCityID]  || [[GloableEngineService sharedInstance].selectedCityID isEqualToString:@"0"]) {
            [RCAppDelegate alertWithCustomizeString:@"抱歉，当前城市暂未开通"];
            return;
        }
        RCRecommendFoodListViewController *foodVC = [[RCRecommendFoodListViewController alloc] init];
        foodVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:foodVC animated:YES];
        [foodVC release];
    }else if ([indexPath row] == 5) {
        if ([NSString stringIsNullOrEmpty:[GloableEngineService sharedInstance].selectedCityID]  || [[GloableEngineService sharedInstance].selectedCityID isEqualToString:@"0"]) {
            [RCAppDelegate alertWithCustomizeString:@"抱歉，当前城市暂未开通"];
            return;
        }
        RCActivityDiscountViewController *activityVC = [[RCActivityDiscountViewController alloc] init];
        activityVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:activityVC animated:YES];
        [activityVC release];
    }else if ([indexPath row] == 1) {
        
        RCPointsShopViewController *vc = [[RCPointsShopViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];

    }
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma 
#pragma mark 城市发生变化时候
-(void) currentCityChanged:(NSString *)cityName
{
    
}

#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestParam:(id)requestParam
{
    
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        RCPushMessageDataModel *pushDataModel = (RCPushMessageDataModel *)dataModel;
        self.pushDataModel = pushDataModel;
        if (self.pushDataModel && self.pushDataModel.itemListArray && [self.pushDataModel.itemListArray count] > 0) {
            
            [self createTabeleHeaderView];
            self.t.tableHeaderView = _headerView;
        }
        
    }
}

-(void)headerTransparentButtonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    
    NSInteger index = button.tag - 300;
    RCPushMessageItem *item = [self.pushDataModel.itemListArray safeObjectAtIndex:index];
    if (nil == item) {
        return;
    }
    NewWebviewPluginController *vc = [[NewWebviewPluginController alloc]initWithUrl:[NSURL URLWithString:item.itemUrl]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)deleteButtonClicked{
    self.t.tableHeaderView = nil;
}


#pragma mark -

#pragma mark UIScrollView Delegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int count = _headerPageControl.numberOfPages;
    if (count == 1) {
        return;
    }
    NSInteger pageNumber = 0;
    pageNumber = floor((scrollView.contentOffset.x - scrollView.frame.size.width/3)/scrollView.frame.size.width)+1;
    
    [_headerPageControl setCurrentPage:pageNumber];
//    NSArray *subView = _headerPageControl.subviews;     // UIPageControl的每个点
//    for (int i = 0; i < [subView count]; i++) {
//        UIImageView *dot = [subView safeObjectAtIndex:i];
//        if (![dot isKindOfClass:[UIImageView class]]) {
//            continue;
//        }
//        dot.image = (_headerPageControl.currentPage == i ? [UIImage imageNamed:kSwitchSceneDotActiveImage] : [UIImage imageNamed:kSwitchSceneDotInActiveImage]);
//        dot.bounds = CGRectMake(0, 0, 7, 7);
//    }
    
}


#pragma
#pragma mark Cell Button Click
- (void) didClickType:(NSInteger)index withTitle:(NSString *)title
{
    if ([NSString stringIsNullOrEmpty:[GloableEngineService sharedInstance].selectedCityID] || [[GloableEngineService sharedInstance].selectedCityID isEqualToString:@"0"]) {
        [RCAppDelegate alertWithCustomizeString:@"抱歉，当前城市暂未开通"];
        return;
    }
    if (index == 2) {
        RCTicketBookingVC *vc = [[RCTicketBookingVC alloc] initWithType:TicketType_Train];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
        return;
    }else if (index == 7){
        
        if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
            return;
        }
        
        RCFansListVC *vc = [[RCFansListVC alloc] initWithUserID:nil];
        vc.vcType = FansListVCType_Score;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        return;
    }else if (index == 5){
        RCSearchMoreTagsVC *vc = [[RCSearchMoreTagsVC alloc]initWithParameters:[GloableEngineService sharedInstance].selectedCityID WithType:SearchMoreTagsVCTypeCategory];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        [vc release];
        return;
    }

    RCMerchantListVC *list = [[RCMerchantListVC alloc] initWithMerchantType:title withMerchantID:nil];
    list.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:list animated:YES];
    [list release];

}

-(void)viewDidUnload{
    [super viewDidUnload];
    self.cityLabel = nil;
    self.cityButton = nil;
    self.arrowImageView = nil;
    self.transparentButton = nil;
    self.searchTextField = nil;
}

- (void)sdkUIRecognitionAction
{
    
    // 创建识别控件
    [_searchTextField resignFirstResponder];
    BDRecognizerViewController *tmpRecognizerViewController = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(9, 128) withTheme:[BDVRConfig sharedInstance].theme];
    tmpRecognizerViewController.delegate = self;
    self.recognizerViewController = tmpRecognizerViewController;
    [tmpRecognizerViewController release];
    
    // 设置识别参数
    BDRecognizerViewParamsObject *paramsObject = [[BDRecognizerViewParamsObject alloc] init];
    
    // 开发者信息
    paramsObject.apiKey = API_KEY;
    paramsObject.secretKey = SECRET_KEY;
    
    // 设置识别模式，分为搜索和输入
    if ([BDVRConfig sharedInstance].voiceRecognitionMode == EVoiceRecognitionModeSearch)
    {
        paramsObject.recognitionMode = BDRecognizerRecognitionModeSearch;
    }
    else
    {
        paramsObject.recognitionMode = BDRecognizerRecognitionModeInput;
    }
    
    // 设置显示效果，是否开启连续上屏
    if ([BDVRConfig sharedInstance].resultContinuousShow)
    {
        paramsObject.resultShowMode = BDRecognizerResultShowModeContinuousShow;
    }
    else
    {
        paramsObject.resultShowMode = BDRecognizerResultShowModeWholeShow;
    }
    
    // 设置提示音开关，是否打开，默认打开
    if ([BDVRConfig sharedInstance].uiHintMusicSwitch)
    {
        paramsObject.recordPlayTones = EBDRecognizerPlayTonesRecordPlay;
    }
    else
    {
        paramsObject.recordPlayTones = EBDRecognizerPlayTonesRecordForbidden;
    }
    
    [_recognizerViewController startWithParams:paramsObject];
    [paramsObject release];
}


#pragma mark - BDRecognizerViewDelegate

- (void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerView withResults:(NSArray *)aResults
{
//    resultView.text = nil;
    
    if ([[BDVoiceRecognitionClient sharedInstance] getCurrentVoiceRecognitionMode] == EVoiceRecognitionModeSearch)
    {
        // 搜索模式下的结果为数组，示例为
        // ["公园", "公元"]
        NSMutableArray *audioResultData = (NSMutableArray *)aResults;
//        NSMutableString *tmpString = [[NSMutableString alloc] initWithString:@""];
//        
//        for (int i=0; i < [audioResultData count]; i++)
//        {
//            [tmpString appendFormat:@"%@\r\n",[audioResultData objectAtIndex:i]];
//        }
        if (audioResultData != nil && [audioResultData count] > 0) {
            NSString *searchStr = [audioResultData safeObjectAtIndex:0];
            RCSearchAllVC *vc = [[RCSearchAllVC alloc]initWithTitle:searchStr];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];
        }
        
    }
    else
    {
        // 输入模式下的结果为带置信度的结果，示例如下：
        //  [
        //      [
        //         {
        //             "百度" = "0.6055192947387695";
        //         },
        //         {
        //             "摆渡" = "0.3625582158565521";
        //         },
        //      ]
        //      [
        //         {
        //             "一下" = "0.7665404081344604";
        //         }
        //      ],
        //   ]
//        NSString *tmpString = [[BDVRConfig sharedInstance] composeInputModeResult:aResults];
        
    }
}

@end
